package com.elitedatai.mchtest.manage.repository;

import com.elitedatai.mchtest.manage.model.entity.DevicePayloadEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

public interface DevicePayloadRepository extends JpaRepository<DevicePayloadEntity, Integer> {
    Collection<DevicePayloadEntity> findByDeviceSn(String deviceSn);

    @Query("delete from DevicePayloadEntity where deviceSn = :deviceSn")
    void deleteByDeviceSn(String deviceSn);

    @Modifying
    @Transactional
    @Query("UPDATE DevicePayloadEntity d SET d.firmwareVersion = :firmwareVersion WHERE d.deviceSn = :droneSn AND d.payloadSn = :payloadSn")
    int updateFirmwareVersionByDeviceSnAndPayloadSn(@Param("firmwareVersion") String firmwareVersion, @Param("droneSn") String droneSn, @Param("payloadSn") String payloadSn);

    DevicePayloadEntity findByPayloadSn(String payloadSn);

    // 添加根据载荷SN集合删除的方法
    @Modifying
    @Transactional
    @Query("DELETE FROM DevicePayloadEntity d WHERE d.payloadSn IN :payloadSns")
    int deleteByPayloadSnIn(@Param("payloadSns") Collection<String> payloadSns);
}
